using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace blogapi
{
    public partial class novyprispevek : Form
    {
        public novyprispevek(publy pub,nastaveni nast,pages pag)
        {
            InitializeComponent();
            pb = pub;
            def = button1.BackColor;
            nas = nast;
            kats = new kategorie(nas,textBox3,pag);
            setlang();
        }

        kategorie kats;
        publy pb;
        nastaveni nas;

        public void nastav(int id,bool autobr)
        {
            if (id == -1)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                this.Tag = -1;
            }
            else
            {
                prispevek pris = (prispevek)pb.prispevkypole[id];
                textBox1.Text = pris.titulek;
                textBox2.Text = pris.text;
                textBox3.Text = pris.kate();
                this.Tag = id;
            }
            checkBox1.Checked = autobr;
            button1.BackColor = def;
            button2.BackColor = def;
            button3.BackColor = def;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text=="")
            {
                MessageBox.Show(nas.getlang("potrebnapole"),nas.getlang("nevyplnenapole"));
                return;
            }
            if (checkBox1.Checked)
            {
                textBox2.Text = textBox2.Text.Replace("\r\n", "<br />");
            }
            if ((int)this.Tag!= -1)
            {
                pb.prispevkypole.RemoveAt((int)this.Tag);
            }
            prispevek pris = new prispevek(textBox1.Text, textBox2.Text, DateTime.Now.Ticks,textBox3.Text);
            pb.prispevkypole.Add(pris);
            pb.vypis(true);
            pb.Show();
            this.Hide();
        }

        Color def;
        private void button1_Click(object sender, EventArgs e)
        {
            Button tlacitko = (Button)sender;
            int pozice = textBox2.SelectionStart;
            int del = textBox2.SelectionLength;
            string slovo = "";
            if (del != 0)
            {
                slovo = "<" + tlacitko.Tag.ToString() + ">";
                textBox2.Text=textBox2.Text.Insert(pozice,slovo);
                pozice += slovo.Length;
                slovo = "</" + tlacitko.Tag.ToString() + ">";
                textBox2.Text = textBox2.Text.Insert(pozice + del, slovo);
                tlacitko.BackColor = def;
            }
            else
            {
                if (tlacitko.BackColor == def)
                {
                    slovo = "<" + tlacitko.Tag.ToString() + ">";
                    tlacitko.BackColor = Color.BlanchedAlmond;
                }
                else
                {
                    slovo = "</" + tlacitko.Tag.ToString() + ">";
                    tlacitko.BackColor = def;
                }
                textBox2.Text = textBox2.Text.Insert(pozice, slovo);
                pozice += slovo.Length;
            }
            textBox2.Select(pozice, del);
            textBox2.Focus();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int pozice = textBox2.SelectionStart;
            int del = textBox2.SelectionLength;
            textBox2.Text = textBox2.Text.Insert(pozice, "<br />");
            textBox2.Select(pozice, del+6);
            textBox2.Focus();
        }

        private void checkBox1_CheckStateChanged(object sender, EventArgs e)
        {
            button4.Visible = checkBox1.Checked ^ true;
        }

        public void setlang()
        {
            menuItem1.Text = nas.getlang("zpet");
            menuItem2.Text = nas.getlang("dalsi");
            label1.Text = nas.getlang("titulekpris");
            label2.Text = nas.getlang("textpris");
            checkBox1.Text = nas.getlang("autodrad");
            Text = "W - "+nas.getlang("newpost");
            label3.Text = nas.getlang("kategorieoddelte");
            kats.setlang();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            kats.stahuj(false);
            kats.Show();
        }
    }
}